<template>
  <div class="app-container">
    <h1>欢迎使用点餐后台管理系统</h1>
    <el-row>
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header">
            <span><i class="el-icon-coffee-cup"></i> 各类菜品数量统计</span>
          </div>
          <div id="category" class="chart"></div>
        </el-card>
      </el-col>
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header">
            <span><i class="el-icon-coffee-cup"></i> 菜品总销量top8</span>
          </div>
          <div id="orderNum" class="chart"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style>
h1 {
  text-align: center;
  font-size: 40px;
}

.chart {
  height: 400px;
  width: 600px;
}
</style>

<script>
import { listCategory } from "@/api/rms/category";
import { getDishesAndAccount } from "@/api/rms/dishes";

export default {
  data() {
    return {

    }
  },

  created() {

  },

  mounted() {
    this.getDataByCategory();
    this.getDishesAccount();
  },

  methods: {
    getDataByCategory() {
      listCategory(null).then(response => {
        let categoryData = response.rows;
        if (categoryData.length <= 0) return;

        let data = [];
        for (let i in categoryData) {
          let temp = {};
          temp.name = categoryData[i].categoryName;
          temp.value = categoryData[i].dishesCount;
          data.push(temp);
        }

        let elementId = 'category';
        this.initPieChart(elementId, data);
      })
    },

    getDishesAccount() {
      getDishesAndAccount().then(response => {
        let dishesAndAccount = response.data;
        if (dishesAndAccount.length <= 0) return;

        let xData = [];
        let yData = [];
        for (let i in dishesAndAccount) {
          if (i === 8) break; // 节省空间，暂时展示top8
          xData[i] = dishesAndAccount[i].name;
          yData[i] = dishesAndAccount[i].value;
        }

        let elementId = 'orderNum';
        this.initLineChart(elementId, xData, yData);
      })
    },

    initPieChart(elementId, data) {
      let myChart = this.$echarts.init(document.getElementById(elementId));
      let option;

      option = {
        legend: {
          top: 'bottom'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c}'
        },
        series: [
          {
            name: 'Nightingale Chart',
            type: 'pie',
            radius: [70, 160],
            center: ['50%', '50%'],
            // roseType: 'area',
            itemStyle: {
              borderRadius: 8
            },
            data: data
          }
        ]
      };
      option && myChart.setOption(option);
    },

    initLineChart(elementId, xData, yData) {
      let myChart = this.$echarts.init(document.getElementById(elementId));
      let option;

      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '1%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: xData,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '总销量',
            type: 'bar',
            barWidth: '70%',
            data: yData,
            itemStyle: {
              normal: {
                color: function(params) {
                  let colorList = ['#0E19E1','#D600AC', '#FF0072', '#FF6D46', '#FFBA3C','#F9F871', '#00D19F', '#7F6FA1'];
                  return colorList[params.dataIndex]
                }
              }
            }
          }
        ]
      };

      option && myChart.setOption(option);
    }
  }
}

</script>
